package keeto.com.vn.classified.widgets;

import java.io.File;

import keeto.com.vn.classified.ui.HandBookActivity;
import keeto.com.vn.classified.ui.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

public class ChangeAvatarDialog extends Dialog implements android.view.View.OnClickListener{
	private Button btn_gallery;
	private Button btn_camera;
	private LinearLayout llParent;
	private Context context;
	private Uri path;

	public ChangeAvatarDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
	}

	public ChangeAvatarDialog(Context context, Uri pathimg) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.path = pathimg;
	}

	
	protected ChangeAvatarDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.change_avatar_layout);
		llParent = (LinearLayout)findViewById(R.id.parent_change_avatar);
		btn_gallery = (Button)findViewById(R.id.btn_gallery);
		btn_camera = (Button)findViewById(R.id.btn_portal_camera);
		btn_gallery.setOnClickListener(this);
		btn_camera.setOnClickListener(this);
		this.setCanceledOnTouchOutside(false);
		Rect displayRectangle = new Rect();
		Window window = this.getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
		llParent.setMinimumWidth((int) (displayRectangle.width() * 0.9f));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_gallery:
			Intent getImageFromGalleryIntent = new Intent(
					Intent.ACTION_PICK,
					MediaStore.Images.Media.INTERNAL_CONTENT_URI);
			((Activity) context).startActivityForResult(getImageFromGalleryIntent,HandBookActivity.PICK_FROM_CALLERY);
			this.dismiss();
			break;
		case R.id.btn_portal_camera:
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		
			intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, path);

			try {
				intent.putExtra("return-data", true);
				((Activity) context).startActivityForResult(intent, HandBookActivity.CAMERA_RESULT);
			} catch (ActivityNotFoundException e) {
				e.printStackTrace();
			}
			this.dismiss();
			break;

		default:
			break;
		}
	}
}
